package com.android.bluetooth;

import java.text.DecimalFormat;
import java.util.HashMap;


public class tichiDataAnalysisUtil {

	static String strWeight = null; //��ǰ����
	static String strEarNum = null; //����
	static String strStaNum = null; //վ��
	static String strLength = null; //�峤
	static String strHeight = null; //���
	static String strChest  = null; //��Χ
	static String strDate   = null; //���� 0910   9��10��
	static String strTime   = null; //ʱ�� 2018   20��18��
	
	void tichiDataAnalysisUtil() {
		initResData();
	}
	
	private static void initResData() {
		strWeight = null; //��ǰ����
		strEarNum = null; //����
		strStaNum = null; //վ��
		strLength = null; //�峤
		strHeight = null; //���
		strChest  = null; //��Χ
		strDate   = null; //���� 0910   9��10��
		strTime   = null; //ʱ�� 2018   20��18��
	}
	
	private static int bytes2Int(byte[] data, int start, int len) {
        int sum = 0;
        int end = start + len;
        for (int i = start; i < end; i++) {
            int n = ((int)data[i]) & 0xff;
            n <<= (--len) * 8;
            sum += n;
        }
        return sum;
    }
	
	private static String getWeight(byte[] data, int start, int len) {
		DecimalFormat df = new DecimalFormat("######0.00");
		return String.valueOf(df.format((double)bytes2Int(data, start, len)/100));
	}
	
	private static String getEarNum(byte[] data, int start, int len) {
		
		return String.format("%04d", Integer.parseInt(String.valueOf(bytes2Int(data, start, 2)))) + " " + 
		       String.format("%04d", Integer.parseInt(String.valueOf(bytes2Int(data, start+2, 2)))) + " " + 
		       String.format("%04d", Integer.parseInt(String.valueOf(bytes2Int(data, start+4, 2)))) + " " + 
		       String.format("%03d", Integer.parseInt(String.valueOf(bytes2Int(data, start+6, 2))));
	}
	
	private static String getStaNum(byte[] data, int start, int len) {
		return String.format("%d", Integer.parseInt(String.valueOf(bytes2Int(data, start, len))));
	}
	
	private static String getLength(byte[] data, int start, int len) {
		DecimalFormat df = new DecimalFormat("######0.00");
		
		byte[] byteArray = new byte[4];
		byteArray[0] = data[start+2];
		byteArray[1] = data[start+3];
		byteArray[2] = data[start];
		byteArray[3] = data[start+1];
		
		if((byteArray[0] == byteArray[1]) && (byteArray[0] == 0x00)) {
			return String.valueOf(df.format((double)bytes2Int(byteArray, 0, 4)));
		}
		
		return String.valueOf(df.format((double)bytes2Int(byteArray, 0, 4)/100));
	}
	
	private static String getHeight(byte[] data, int start, int len) {
		DecimalFormat df = new DecimalFormat("######0.00");
		
		byte[] byteArray = new byte[4];
		byteArray[0] = data[start+2];
		byteArray[1] = data[start+3];
		byteArray[2] = data[start];
		byteArray[3] = data[start+1];
		
		if((byteArray[0] == byteArray[1]) && (byteArray[0] == 0x00)) {
			return String.valueOf(df.format((double)bytes2Int(byteArray, 0, 4)));
		}
		
		return String.valueOf(df.format((double)bytes2Int(byteArray, 0, 4)/100));
	}
	
	private static String getChest(byte[] data, int start, int len) {
		DecimalFormat df = new DecimalFormat("######0.00");
		
		byte[] byteArray = new byte[4];
		byteArray[0] = data[start+2];
		byteArray[1] = data[start+3];
		byteArray[2] = data[start];
		byteArray[3] = data[start+1];
		
		if((byteArray[0] == byteArray[1]) && (byteArray[0] == 0x00)) {
			return String.valueOf(df.format((double)bytes2Int(byteArray, 0, 4)));
		}
		
		return String.valueOf(df.format((double)bytes2Int(byteArray, 0, 4)/100));
	}
	
	private static String getDate(byte[] data, int start, int len) {
		return String.format("%02d月%02d日", bytes2Int(data, start, len)/100, bytes2Int(data, start, len)%100);
	}

	private static String getTime(byte[] data, int start, int len) {
		return String.format("%02d点%02d分", bytes2Int(data, start, len)/100, bytes2Int(data, start, len)%100);
	}

	static boolean ansyBlueData(byte[] data) {
		initResData();
		
		if(data.length < 31) {
			return false;
		}
		
		strWeight = getWeight(data, 3, 2);
		
		strEarNum = getEarNum(data, 5, 8);
		
		strStaNum = getStaNum(data, 13, 2);
		
		strLength = getLength(data, 15, 4);
		
		strHeight = getHeight(data, 19, 4);
		
		strChest = getChest(data, 23, 4);

		strDate = getDate(data, 27, 2);
		
		strTime = getTime(data, 29, 2);
		
		return true;
	}


	public static String getString(){
		return "strWeight = "+strWeight +" \\n strEarNum = "+strEarNum +" \\n strStaNum = "+strStaNum
				+" \\n strLength = "+strLength+" \\n strHeight = "+strHeight+" \\n strChest = "+strChest
				+" \\n strDate = "+strDate+" \\n strDate = "+strDate;
	}
}
